package com.eascs.spider.hr.user.service;

import java.util.ArrayList;
import java.util.List;

import org.bson.Document;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.spider.hr.common.service.BaseSevice;
import com.eascs.spider.hr.user.entity.Condition;
import com.eascs.spider.hr.user.entity.Result;
import com.eascs.spider.hr.user.entity.ResultProj;
import com.eascs.spider.hr.util.MongodbUtil;
import com.google.gson.Gson;

@Service
public class HrService extends BaseSevice{

	private static Logger logger = LoggerFactory.getLogger(HrService.class);
	
	@Transactional(transactionManager = "ticketTransactionManager")
	public ResultProj query(Condition condition){
		int pageSize = 15;
		List<Result> list = new ArrayList<>();
		logger.debug("查询条件，关键词：{}，地区：{},职能：{}", condition.getKeyword(),condition.getArea(),condition.getFunction());
		ResultProj resultProj = MongodbUtil.query51job(condition.getArea(), condition.getKeyword(), 
				condition.getFunction() ,condition.getUpdate_time() ,condition.getAgeMin(), 
				condition.getAgeMax(),condition.getPage()==0?1:condition.getPage(),pageSize);//默认查第一页
		
		 List<Document> list2 = resultProj.getDocuments();
		for(Document document : list2){
			Result result = new Result();
			result.setAddress(document.getString("居住地"));
			result.setAge(document.getString("年龄"));
			result.setEducation(document.getString("学历"));
			result.setFunction	(document.getString("职能"));
			result.setExpect_address(document.getString("期望工作地区"));
			result.setSex(document.getString("性别"));
			result.setSnapshot(document.getString("快照"));
			result.setUrl(document.getString("简历链接"));
			result.setUpdate_time(document.getString("简历更新时间"));
			result.setWork_age(document.getString("工作年限"));
			result.setId(document.getString("ID"));
			result.setKeyword(document.getString("关键词"));
			list.add(result);
			logger.debug("单条简历信息：{}", new Gson().toJson(result));
		}
		logger.info("查询数据量：{}", list.size());
		resultProj.setResults(list);
		return resultProj;
	}

}
